﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Linq
{
    public class ParamModel
    {
        private static readonly ConcurrentDictionary<Type, ParamModel> dic = new();

        public List<ParamItem> List { get; private set; }

        /// <summary>
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <returns></returns>
        public static ParamModel Instance<TModel>()
        {
            var typeFromHandle = typeof(TModel);
            if (dic.TryGetValue(typeFromHandle, out ParamModel value))
                return value;

            lock (typeFromHandle)
            {
                var list = new List<ParamItem>();
                typeFromHandle.GetProperties().ToList()
                              .ForEach(a => list.Add(new ParamItem(a, a.Name)));
                value = new ParamModel
                {
                    List = list
                };

                dic.TryAdd(typeFromHandle, value);
                return value;
            }
        }
    }
}